//index.js
//获取应用实例
const app = getApp()

Page({
	data: {
	  	yzmState:true,
	  	yzmHtml:'发送验证码',
	  	phone:'',
	  	mess:'将发送短信给',
	  	yzm:'',
	  	password:'',
	  	eyeState:true
	},
	onLoad (options) {
	  	this.setData({
	  		phone:options.phone
	  	})
	},
	timeReduce(){
	  	var timer = null,second=60,self = this;
    	var yzmState = false,yzmHtml;
        timer = setInterval(function(){
            second--;
            yzmHtml = second+'s';
            if(second == 0){
            	yzmHtml="发送验证码"
                clearInterval(timer)
                yzmState = true
            }
            self.setData({
            	yzmHtml:yzmHtml,
            	yzmState:yzmState
            })
        },1000)
	},
	sendYzm(){
		var self = this
		var yzmState = self.data.yzmState
		var phone = self.data.phone
		var url = app.globalData.url.findpassword;
		if (yzmState) {
			wx.request({
		      url: url,
		      data: {
		        "cfrom": app.globalData.cfrom,
		        "phone": phone,
		      },
		      method: "GET",
		      success(res) {
		        let data = res.data
		        if(data.status == "_0000"){
		        	self.timeReduce()
		        }else if(data.status == "1001"){
		            wx.showToast({title:"您已经注册过,请直接登陆",icon:'none'})
		        }else{
		        	 wx.showToast({title:data.message,icon:'none'})
		        }
		  	},
		    fail(res) {
		        console.log(res.statusCode)
		      }
		    })
		}
	},
	login (phone,Password) {
		var url =  app.globalData.url.loginPhone
		wx.request({
	     	url: url,
	      	data: {
		        "phone": phone,
	            "cfrom": app.globalData.cfrom,
	            "password": Password
		      },
	      	method: "GET",
	      	success(res) {
		        let data = res.data
		        if(data.status == "_0000"){
		        	var datas = data.data;
		         	var userId = datas.id,userPass = Password;
		          	app.globalData.userId = userId
		          	app.globalData.userPass = userPass
		        	wx.navigateTo({url:'../index/index'})
		        }
		  	},
	      	fail(res) {
		        console.log(res.statusCode)
		      }
		})
	},
	setPassword(){
		var self = this
		var url = app.globalData.url.setpassword
		var yzm = self.data.yzm,password = self.data.password,phone = self.data.phone;
	    if(yzm.length < 6){
	    	wx.showToast({title:"请输入6位数字验证码",icon:'none'})
	        return false
	    };
	    if(password.length < 6){
	    	wx.showToast({title:"密码请设置为6-12位",icon:'none'})
	        return false;
	    };
	    wx.request({
	     	url: url,
	      	data: {
		       "phone": phone,
	            "cfrom": app.globalData.cfrom,
	            "vphone": yzm,
	            "password": password
		    },
	      	method: "GET",
	      	success(res) {
		        let data = res.data
		        if(data.status == "_0000"){
	                self.login(phone,password);
	            }else if(data.status == "_0038"){
	                self.login(phone,password);
	            }else{
	            	wx.showToast({title:data.message,icon:'none'})
	            }
		  	},
	      	fail(res) {
		        console.log(res.statusCode)
		      }
		})
	},
	inputYzm (e) {
	    this.setData({
	      yzm: e.detail.value
	    })
	},
	inputPassword (e) {
		var str = e.detail.value
	    if(str.indexOf(" ") >= 0){
	    	wx.showToast({title:"密码不应该含有空格",icon:'none'})
	    }
	    str = str.replace(/\s+/g,'');
	    this.setData({
	      password: str
	    })
	},
	handlChangePass () {
		var eyeState = this.data.eyeState
		if (eyeState) {
			eyeState = false
		}else{
			eyeState = true
		}
		this.setData({
			eyeState:eyeState
		})
	}
})
